﻿(function (win) {

    var module = angular.module('dashboardModule', ['ngResource', 'portal'], function () { });

    define(['css!./dashboard.css', 'template!./dashboard.html', 'home/dashboard-service'], function (css, template, requireDashboardService) {

        module.controller('dashboardController', ['$scope', '$rootScope', 'dashboardService', 'dataService', '$timeout', function ($scope, $rootScope, dashboardService, dataService, $timeout) {

            dashboardService.getDashboardData(function (data) {
                var wl = data.widgets;
                for (var i = 0; i < wl.length; i++) {
                    if (!wl[i].options)
                        wl[i].options = {};
                    if (!wl[i].options.title)
                        wl[i].options.title = 'Loading...';
                }

                $scope.data = data;
            });

            $scope.widgetLoadComplete = function (id, m) {
                var w = dataService.find({ id: id }, $scope.data.widgets);
                if (w) {
                    $timeout(function () {
                        angular.merge(w.options, m.options);
                    });
                }
            }
        }]);

        return {
            template: template,
            module: 'dashboardModule',
            controller: 'dashboardController'
        }
    });
})(window);